﻿using Newtonsoft.Json;
using QG.FrontDesk.BLL.IService;
using QG.FrontDesk.com.Models;
using QG.FrontDesk.Core;
using QG.FrontDesk.Core.Encrypt;
using QG.FrontDesk.Model;
using System;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;

namespace LaGou.Controllers
{
    public class LoginController : Controller
    {
        private IUserInfoService userInfoService;

        private ICompanyService _companyService;
        public LoginController(IUserInfoService userInfoService, ICompanyService companyService)
        {
            this.userInfoService = userInfoService;
            _companyService = companyService;
        }
        /// <summary>
        /// 登入页面
        /// </summary>
        /// <returns></returns>
        // GET: Login
        public ActionResult Login()
        {

            return View();
        }
        public ActionResult Register()
        {
            return View();
        }
        /// <summary>
        /// 邮箱登录
        /// </summary>
        /// <param name="userInfo"></param>
        /// <param name="opr"></param>
        /// <returns></returns>
        public JsonResult GetLogin(string Email, string UserPwd, bool type)
        {
            ResponseResult opercte = new ResponseResult();
            UserInfo user = null;
            Company company = null;
            UserPwd = MD5Encrypt.Encrypt(UserPwd);//加密
            if (type)
            {
                user = userInfoService.Getlogin(new UserInfo()
                {
                    Email = Email,
                    UserPwd = UserPwd
                });
                //存储在session里面     
                DataContext<UserInfo>.context.model = user;
            }
            else
            {
                company = _companyService.GetCompany(Email, UserPwd);
                DataContext<Company>.context.model = company;
            }
            if (user != null || company != null)
            {
                opercte.success = true;
                FormsAuthenticationTicket ticket = null;
                if (type)
                {
                    //拼接要存的管理员id和登录名称
                    string userData = JsonConvert.SerializeObject(user);
                    //数据放入ticket
                    ticket = new FormsAuthenticationTicket(1, "求职", DateTime.Now, DateTime.Now.AddMinutes(60), false, userData);
                }
                else
                {
                    //拼接要存的管理员id和登录名称
                    string userData = JsonConvert.SerializeObject(company);
                    //数据放入ticket
                    ticket = new FormsAuthenticationTicket(1, "公司", DateTime.Now, DateTime.Now.AddMinutes(60), false, userData);
                }
                //数据加密
                string enyTicket = FormsAuthentication.Encrypt(ticket);
                //创建cookie
                HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName, enyTicket);
                //写入cookie
                System.Web.HttpContext.Current.Response.Cookies.Add(cookie);
            }
            else
            {
                FormsAuthentication.SignOut();
            }

            return Json(opercte);
        }
        /// <summary>
        /// 退出登录
        /// </summary>
        /// <returns></returns>
        public ActionResult LoginOut()
        {
            //清除cookie
            FormsAuthentication.SignOut();
            //清除session
            DataContext<UserInfo>.context.model = null;
            DataContext<Company>.context.model = null;
            //跳转到管理员登录页
            return RedirectToAction("Login");
        }
        /// <summary>
        /// 注册用户
        /// </summary>
        /// <param name="userInfo"></param>
        /// <returns></returns>
        public JsonResult AddUserinfo(UserInfo userInfo)
        {
            userInfo = new UserInfo()
            {
                Email = userInfo.Email,
                UserPwd = MD5Encrypt.Encrypt(userInfo.UserPwd),
                Phone = userInfo.Phone,
                UserName = userInfo.UserName
            };
            ResponseResult opercte = new ResponseResult();
            opercte.success = userInfoService.AddUserInfo(userInfo);
            return Json(opercte);
        }
        /// <summary>
        ///  手机登录
        /// </summary>
        /// <param name="verificationCode"></param>
        /// <returns></returns>
        public JsonResult QueryVerificationCode(VerificationCode verificationCode, bool opr)
        {
            ResponseResult opercte = new ResponseResult();
            VerificationCode verification = userInfoService.QueryVerificationCode(verificationCode);
            if (verification != null)
            {
                //通过手机号得到对应的用户信息
                UserInfo userInfo = userInfoService.GetUserByPhone(verification.Phone);
                //存储在session里面                                       
                DataContext<UserInfo>.context.model = userInfo;
                opercte.success = true;
                //拼接要存的管理员id和登录名称
                string userData = JsonConvert.SerializeObject(userInfo);
                //数据放入ticket
                FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1, "求职", DateTime.Now, DateTime.Now.AddMinutes(60), false, userData);

                //数据加密
                string enyTicket = FormsAuthentication.Encrypt(ticket);
                //创建cookie
                HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName, enyTicket);
                //写入cookie
                System.Web.HttpContext.Current.Response.Cookies.Add(cookie);
            }
            return Json(opercte);
        }


    }
}